﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;//FindControl
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.VDDIC_Gamma
{
    public partial class ICNA3511 : Form
    {
        public ICNA3511()
        {
            InitializeComponent();

        }

        frmMain frmHelp = null;
        public ICNA3511(frmMain ICNA3511)
        {
            frmHelp = ICNA3511;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体    
            string[] s = { ".bmp" };
            this.ImageType = new List<string>(s);

        }


        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过   
        private bool Listening = false;//是否没有执行完invoke相关操作
        private byte[] Rec_tmp = new byte[5];//
        public byte start_trans_flag = 0;
        public byte continueflag = 0;
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            //if (MainForm.IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (buffer[0] == 0x0B)          //返回 STM32内部 flash 的配置文件数据
                    {
                        if (buffer.Count >= (Int16)(buffer[2] << 8) + (buffer[3]) + 7)
                        {
                            Rec_tmp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            byte[] crcTemp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            buffer.CopyTo(0, crcTemp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 2] != CRCH && buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 1] != CRCL) //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//正确分析一条数据，从缓存中移除数据。
                            }

                        }
                    }
                    else if (buffer[0] == 0xFE)
                    {
                        Rec_tmp = new byte[buffer[2] + 5];
                        byte[] crcTemp = new byte[buffer[2] + 5];
                        buffer.CopyTo(0, crcTemp, 0, buffer[2] + 5);

                        CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                        if (buffer[buffer[2] + 5 - 2] != CRCH && buffer[buffer[2] + 5 - 1] != CRCL)     //CRC校验出错
                        {
                            buffer.RemoveRange(0, buffer.Count);
                        }
                        else
                        {
                            buffer.CopyTo(0, Rec_tmp, 0, buffer[2] + 5);//复制一条完整数据到具体的数据缓存   
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer[2] + 5);//正确分析一条数据，从缓存中移除数据。
                            if (Rec_tmp[4] == 0)
                                continueflag = 1;
                        }
                    }
                    else if (buffer[0] == 0x55)     //STM32 更新bin文件成功  返回
                    {
                        if (buffer.Count >= (buffer[2] + 5))
                        {
                            Rec_tmp = new byte[(buffer[2] + 5)];
                            byte[] crcTemp = new byte[(buffer[2] + 5)];
                            buffer.CopyTo(0, crcTemp, 0, (buffer[2] + 5));

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(buffer[2] + 5) - 2] != CRCH && buffer[(buffer[2] + 5) - 1] != CRCL)       //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (buffer[2] + 5));//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (buffer[2] + 5));//正确分析一条数据，从缓存中移除数据。
                                switch (Rec_tmp[3])
                                {
                                    case 0x00:
                                        {
                                            start_trans_flag = 1;
                                        }
                                        break;
                                    case 0x01:
                                        {
                                            if (frmHelp.comPort.IsOpen == true) frmHelp.comPort.Close();
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            start_trans_flag = 0;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }

                        }
                    }
                    else if (((buffer[0] == 0x1E) || (buffer[0] == 0x70) || (buffer[0] == 0x0E) || (buffer[0] == 0x0D) || (buffer[0] == 0x0A) || (buffer[0] == 0x09) || (buffer[0] == 0x08) || (buffer[0] == 0x19) || (buffer[0] == 0x29) || (buffer[0] == 0x2A) || (buffer[0] == 0x09) || (buffer[0] == 0x2E) || (buffer[0] == 0x2B) || (buffer[0] == 0x2C) || (buffer[0] == 0x2D) || (buffer[0] == 0x2F) || (buffer[0] == 0x30) || (buffer[0] == 0x31) || (buffer[0] == 0x3C) || (buffer[0] == 0x3D) || (buffer[0] == 0x4A) || (buffer[0] == 0x4C) || (buffer[0] == 0x4D) || (buffer[0] == 0x66)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        //frmHelp.T1.Enabled = false; //停止超时计数
                        T1.Enabled = false; //停止超时计数

                        //------------------------------------------------------------------------------------------------------------------------
                        if ((Rec_tmp[0] == 0x09) || (Rec_tmp[0] == 0x19) || (Rec_tmp[0] == 0x29) || (Rec_tmp[0] == 0x2e))  //RM67195 & RM67295 & RM67198&RM69298
                        {
                            ;
                        }

                        //返回接受到的返回值设定并返回相应文本
                        // else if (Rec_tmp[0] == 0x60)      //NT37701
                        else if (Rec_tmp[0] == 0x70)      //ICNA3511
                        {
                            string tmp = "";
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            Target_Value_3511.Text = Target_Value_3511.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            Target_Value_3511.Text = Target_Value_3511.Text + "GAMMA \t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            Target_Value_3511.Text = Target_Value_3511.Text + "GAMMA \t写 ERROR！\r\n";
                                    }
                                    break;


                                case 0x09:
                                    {//r
                                        if (Setting.TempRecord.ICNA3511CmdType == 0x03)
                                        {
                                            //总共25个绑点  最大到24
                                            for (int i = 0; i < 25; i++)
                                            {
                                                Name_2_Value_3("ICNA3511R" + (i + 1).ToString(), Decimal.Parse((Rec_tmp[2 * i + 4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[2 * i + 5].ToString()));
                                            }
                                            Target_Value_3511.Text = Target_Value_3511.Text + "红 Gamma数据\t读 OK！\r\n";

                                        }
                                        //g
                                        if (Setting.TempRecord.ICNA3511CmdType == 0x04)
                                        {
                                            //总共25个绑点  最大到24
                                            for (int i = 0; i < 25; i++)
                                            {
                                                Name_2_Value_3("ICNA3511G" + (i + 1).ToString(), Decimal.Parse((Rec_tmp[2 * i + 4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[2 * i + 5].ToString()));
                                            }
                                            Target_Value_3511.Text = Target_Value_3511.Text + "绿 Gamma数据\t读 OK！\r\n";

                                        }
                                        //b
                                        if (Setting.TempRecord.ICNA3511CmdType == 0x05)
                                        {
                                            //总共25个绑点  最大到24
                                            for (int i = 0; i < 25; i++)
                                            {
                                                Name_2_Value_3("ICNA3511B" + (i + 1).ToString(), Decimal.Parse((Rec_tmp[2 * i + 4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[2 * i + 5].ToString()));
                                            }
                                            Target_Value_3511.Text = Target_Value_3511.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }

                                    }
                                    break;








                                //case 0x11:
                                //    {
                                //        switch (Rec_tmp[3])
                                //        {
                                //            case 0x01:
                                //            case 0x02:
                                //            case 0x03:
                                //                {
                                //                    Target_Value_3511.Text = Target_Value_3511.Text + "Gamma切换成功！\r\n";
                                //                }
                                //                break;
                                //            default: break;
                                //        }
                                //        /*if (Rec_tmp[4] == 0x00)
                                //        {
                                //            Target_Value_3511.Text = Target_Value_3511.Text + "Gamma切换成功！\r\n";
                                //        }*/
                                //    }
                                //    break;



                                //case 0x08:
                                //    {
                                //        if (Rec_tmp[4] == 0x00)
                                //            Target_Value_3511.Text = Target_Value_3511.Text + "写Gamma寄存器\tOK！\r\n";
                                //    }
                                //    break;
                                //case 0x02:
                                //    {
                                //        //if (Setting.TempRecord.NT37701CmdType == 0x02)  //register read
                                //        {
                                //            //string tmp = "";
                                //            for (int i = 3; i < (Setting.TempRecord.cmdLen - 3); i++)
                                //            {
                                //                tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";
                                //            }
                                //            tmp = tmp + String.Format("{0:X2}", Rec_tmp[Setting.TempRecord.cmdLen - 3]);
                                //            NT37701_txtRegData.Text = tmp;
                                //            Target_Value_3511.Text = Target_Value_3511.Text + "寄存器\t读 OK！\r\n";
                                //        }
                                //    }
                                //    break;
                                //case 0x08:
                                //    {
                                //        if(T18.Enabled == true)
                                //        {

                                //        }
                                //        else
                                //        {
                                //            if (Rec_tmp[4] == 0x00)
                                //                textBox1.Text = textBox1.Text + "GAMMA \t写 OK！\r\n";
                                //            else if (Rec_tmp[4] == 0x02)
                                //                textBox1.Text = textBox1.Text + "GAMMA \t写 ERROR！\r\n";
                                //        }



                                //    }
                                //    break;
                                //case 0x07:
                                //    {
                                //    }
                                //    break;




                                //回读值模板简化
                                case 0xa9:
                                    {  //R
                                        if (Setting.TempRecord.NT37700CCmdType == 0x03)
                                        {
                                            //总共25个绑点  最大到24
                                            for (int i = 0; i < 25; i++)
                                            {
                                                Name_2_Value_3("NT37700C_R" + i.ToString(), Decimal.Parse((Rec_tmp[2 * i + 4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[2 * i + 5].ToString()));
                                            }

                                            //NT37700C_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            //……
                                            //NT37700C_R33.Value = Decimal.Parse((Rec_tmp[70] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[71].ToString());
                                            //frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                            Target_Value_3511.Text = Target_Value_3511.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        //G
                                        else if (Setting.TempRecord.NT37700CCmdType == 0x04)
                                        {
                                            for (int i = 0; i < 25; i++)
                                            {
                                                Name_2_Value_3("NT37700C_G" + i.ToString(), Decimal.Parse((Rec_tmp[2 * i + 4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[2 * i + 5].ToString()));
                                            }
                                            //frmHelp.Target_Value_3511.Text = frmHelp.Target_Value_3511.Text + "绿 Gamma数据\t读 OK！\r\n";
                                            Target_Value_3511.Text = Target_Value_3511.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        //B
                                        else if (Setting.TempRecord.NT37700CCmdType == 0x05)
                                        {
                                            for (int i = 0; i < 25; i++)
                                            {
                                                Name_2_Value_3("NT37700C_B" + i.ToString(), Decimal.Parse((Rec_tmp[2 * i + 4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[2 * i + 5].ToString()));
                                            }
                                            //frmHelp.Target_Value_3511.Text = frmHelp.Target_Value_3511.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                            Target_Value_3511.Text = Target_Value_3511.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;






                                case 0x0A:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            Target_Value_3511.Text = Target_Value_3511.Text + "GAMMA \t传输 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02)
                                            Target_Value_3511.Text = Target_Value_3511.Text + "GAMMA \t传输 ERROR！\r\n";
                                    }
                                    break;

                                default: break;

                            }
                        }


                        ////---------------------------------------------------Demura----------------------------------------
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            if ((Rec_tmp[7] == 0x2E) || (Rec_tmp[7] == 0x3C))   // 297/298/299  //350
                            {
                                if (Rec_tmp[7] == 0x2E)// 297/298/299
                                {
                                    Target_Value_3511.Text = Target_Value_3511.Text + "297 298 299\r\n";
                                }
                                else if (Rec_tmp[7] == 0x3C) //350
                                {
                                    Target_Value_3511.Text = Target_Value_3511.Text + "350\r\n";
                                }
                                switch (Rec_tmp[1])
                                {
                                    case 0x01:
                                        {
                                            Target_Value_3511.Text = Target_Value_3511.Text + "Demura RAM Enable\r\n";
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            Target_Value_3511.Text = Target_Value_3511.Text + "Demura RAM Disable\r\n";
                                        }
                                        break;
                                    case 0x03:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    Target_Value_3511.Text = Target_Value_3511.Text + "Demura RAM write Flash complete ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    Target_Value_3511.Text = Target_Value_3511.Text + "Demura RAM write Flash1 complete ! \r\n";
                                                }
                                                else if (Rec_tmp[4] == 0x00)
                                                {
                                                    Target_Value_3511.Text = Target_Value_3511.Text + "ERROR: write Flash1 timeout ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x0A:///350 only
                                        {
                                            if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    Target_Value_3511.Text = Target_Value_3511.Text + "Demura RAM write Flash2 complete ! \r\n";
                                                    MessageBox.Show("载入完成！");
                                                }
                                                else if (Rec_tmp[4] == 0x00)
                                                {
                                                    Target_Value_3511.Text = Target_Value_3511.Text + "ERROR: write Flash2 timeout ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x04:
                                        {
                                            Target_Value_3511.Text = Target_Value_3511.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                        }
                                        break;


                                    default:
                                        break;
                                }
                            }
                        }

                        Target_Value_3511.Focus();//获取焦点
                        Target_Value_3511.Select(Target_Value_3511.TextLength, 0);//光标定位到文本最后
                        Target_Value_3511.ScrollToCaret();//滚动到光标处
                        if (Target_Value_3511.Lines.Length > 200) Target_Value_3511.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;
            }

        }











        //代码开始的地方
        private double[] Target_Lum_Value_ICNA3511 = new double[25] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };//第一个是255的目标值点
        private int[] Target_Node_Value_ICNA3511 = new int[25] { 3, 5, 8, 11, 13, 16, 19, 21, 25, 30, 40, 50, 70, 90, 110, 130, 150, 170, 190, 200, 210, 220, 224, 239, 255 };//第一个是255的目标值点
        private double[] Gary_Number_ICNA3511_Lum_off = new double[25] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };//设置每个绑点的off

        //计算目标值  15绑点的目标值
        //Gamma_2x  是2.2的指数
        //Gamma_Lum  是目标亮度
        double Gamma_2x = 2.2; //gamma 2.2的值
        double Gamma_Lum = 450;//调节的目标值
        private void btn3511_Calculate_Click(object sender, EventArgs e)
        {//计算目标亮度
            for (int i = 0; i < 25; i++)
            {
                Target_Lum_Value_ICNA3511[i] = (Math.Pow((Target_Node_Value_ICNA3511[i] / 255.0), Gamma_2x) * Gamma_Lum);
            }
            Display_Target_Number();
        }
        private void Display_Target_Number()
        {
            for (int i = 0; i < 25; i++)
            {
                Target_Value_3511.Text += Target_Node_Value_ICNA3511[i].ToString() + ":" + Target_Lum_Value_ICNA3511[i].ToString("F2") + "±" + Gary_Number_ICNA3511_Lum_off[i].ToString() + "\r\n";
            }
        }

        private void txt_clc_Click(object sender, EventArgs e)
        {
            Target_Value_3511.Text = "";
        }

        CRC16 CRCData = new CRC16();

        private byte[] Send_buf = new byte[1];

        private void ICNA3511_RegWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Int32 DataNum;
            string strLine = Convert.ToString(ICNA3511_txtRegData.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            switch (btnTmp.Name)
            {
                case "ICNA3511_RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x70;//ICAN3511

                        if (ICNA3511_rbt_LP.Checked == true)
                        { Send_buf[1] = 0x01; }
                        else if (ICNA3511_rbt_HS.Checked == true)
                        { Send_buf[1] = 0x81; }

                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "ICNA3511_51RegW":
                    {
                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x70;
                        Send_buf[1] = 0x03;
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + arr.Length);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                case "ICNA3511_RegR":
                    {
                        Setting.TempRecord.ICNA3511CmdType = 0x02;
                        try
                        {
                            DataNum = Convert.ToInt32(ICNA3511_txtDataNum.Text) + 1;
                            Setting.TempRecord.cmdLen = DataNum + 5;
                            Send_buf = new byte[DataNum + 5];
                            Send_buf[0] = 0x70; //ICN3511
                            Send_buf[1] = 0x02;
                            Send_buf[2] = byte.Parse(Convert.ToString(DataNum));
                            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());  //cmd
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + DataNum - 2], out Send_buf[5 + DataNum - 1]);
                        }



                        catch
                        {
                            MessageBox.Show("读取长度有误，请检查", "错误");
                            break;
                        }
                        try
                        {
                            frmHelp.comPort.Write(Send_buf, 0, 5 + DataNum);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                    }
                    break;
                default: break;
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }

        private void ICNA3511_trackBar_Scroll(object sender, EventArgs e)
        {
            ICNA3511_label.Text = ICNA3511_trackBar.Value.ToString() + "阶";
            ICNA3511_txtRegData.Text = "51," + ICNA3511_trackBar.Value.ToString("x2");
            ICNA3511_RegWR_Click(ICNA3511_51RegW, e);
        }


        ////每一位的含义
        //Send_buf[0]//IC 
        //Send_buf[1]//命令，读写存载
        //Send_buf[2]//cmdLen
        //Send_buf[3]//gamma-slecet
        //Send_buf[4]- Send_buf[Last - 2]//gamma绑点对应的高低位
        //Send_buf[Last-1]//Last-1
        //Send_buf[Last]//Last
        //除绑点外的buf为6个


        private void ICNA3511_GammaWR_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[56];
            Setting.TempRecord.cmdLen = 56;
            Send_buf[2] = 56;//25*2+1
            //Send_buf[0] = 0x57; //ICNA3310
            Send_buf[0] = 0x70; //ICNA3511
            if (ICNA3511_Gamma.Value == 1) { Send_buf[3] = 0x00; }//gamma-1
            else if (ICNA3511_Gamma.Value == 2) { Send_buf[3] = 0x01; }//gamma-2
            else if (ICNA3511_Gamma.Value == 3) { Send_buf[3] = 0x02; }//gamma-3
            else if (ICNA3511_Gamma.Value == 4) { Send_buf[3] = 0x03; }//gamma-4
            switch (btnTmp.Name)
            {
                case "ICNA3511_GammaW_RED":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x10; //RED
                        int j = 1;
                        for (int i = 4; i < (25) * 2 + 4 - 1; i = i + 2)  //25个绑点
                        {
                            Send_buf[i] = Name_2_Value_High("ICNA3511R" + j.ToString());//从R1开始
                            Send_buf[i + 1] = Name_2_Value_Low("ICNA3511R" + j.ToString());
                            j = j + 1;
                        }

                        #region
                        //Send_buf[4] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R0.Value.ToString()) >> 8);
                        //Send_buf[5] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R0.Value.ToString())) & 0xff);
                        //Send_buf[6] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R1.Value.ToString()) >> 8);
                        //Send_buf[7] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R1.Value.ToString())) & 0xff);
                        //Send_buf[8] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R2.Value.ToString()) >> 8);
                        //Send_buf[9] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R2.Value.ToString())) & 0xff);
                        //Send_buf[10] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R3.Value.ToString()) >> 8);
                        //Send_buf[11] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R3.Value.ToString())) & 0xff);
                        //Send_buf[12] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R4.Value.ToString()) >> 8);
                        //Send_buf[13] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R4.Value.ToString())) & 0xff);
                        //Send_buf[14] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R5.Value.ToString()) >> 8);
                        //Send_buf[15] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R5.Value.ToString())) & 0xff);
                        //Send_buf[16] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R6.Value.ToString()) >> 8);
                        //Send_buf[17] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R6.Value.ToString())) & 0xff);
                        //Send_buf[18] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R7.Value.ToString()) >> 8);
                        //Send_buf[19] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R7.Value.ToString())) & 0xff);
                        //Send_buf[20] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R8.Value.ToString()) >> 8);
                        //Send_buf[21] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R8.Value.ToString())) & 0xff);
                        //Send_buf[22] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R9.Value.ToString()) >> 8);
                        //Send_buf[23] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R9.Value.ToString())) & 0xff);
                        //Send_buf[24] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R10.Value.ToString()) >> 8);
                        //Send_buf[25] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R10.Value.ToString())) & 0xff);
                        //Send_buf[26] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R11.Value.ToString()) >> 8);
                        //Send_buf[27] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R11.Value.ToString())) & 0xff);
                        //Send_buf[28] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R12.Value.ToString()) >> 8);
                        //Send_buf[29] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R12.Value.ToString())) & 0xff);
                        //Send_buf[30] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R13.Value.ToString()) >> 8);
                        //Send_buf[31] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R13.Value.ToString())) & 0xff);
                        //Send_buf[32] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R14.Value.ToString()) >> 8);
                        //Send_buf[33] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R14.Value.ToString())) & 0xff);
                        //Send_buf[34] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R15.Value.ToString()) >> 8);
                        //Send_buf[35] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R15.Value.ToString())) & 0xff);
                        //Send_buf[36] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R16.Value.ToString()) >> 8);
                        //Send_buf[37] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R16.Value.ToString())) & 0xff);
                        //Send_buf[38] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R17.Value.ToString()) >> 8);
                        //Send_buf[39] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R17.Value.ToString())) & 0xff);
                        //Send_buf[40] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R18.Value.ToString()) >> 8);
                        //Send_buf[41] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R18.Value.ToString())) & 0xff);
                        //Send_buf[42] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R19.Value.ToString()) >> 8);
                        //Send_buf[43] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R19.Value.ToString())) & 0xff);
                        //Send_buf[44] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R20.Value.ToString()) >> 8);
                        //Send_buf[45] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R20.Value.ToString())) & 0xff);
                        //Send_buf[46] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R21.Value.ToString()) >> 8);
                        //Send_buf[47] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R21.Value.ToString())) & 0xff);
                        //Send_buf[48] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R22.Value.ToString()) >> 8);
                        //Send_buf[49] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R22.Value.ToString())) & 0xff);
                        //Send_buf[50] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R23.Value.ToString()) >> 8);
                        //Send_buf[51] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R23.Value.ToString())) & 0xff);
                        //Send_buf[52] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R24.Value.ToString()) >> 8);
                        //Send_buf[53] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R24.Value.ToString())) & 0xff);
                        //Send_buf[54] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R25.Value.ToString()) >> 8);
                        //Send_buf[55] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R25.Value.ToString())) & 0xff);
                        //Send_buf[56] = Convert.ToByte(Convert.ToUInt16(ICNA3310C_R26.Value.ToString()) >> 8);
                        //Send_buf[57] = Convert.ToByte((Convert.ToUInt16(ICNA3310C_R26.Value.ToString())) & 0xff);
                        #endregion


                        //算buf的值验证一下
                        //for (int k = 0; k < 58;k++)
                        //{
                        //    Target_Value_3511.Text += k.ToString() + ":" + String.Format("{0:X}", Send_buf[k]).ToString() + "\r\n";

                        //    //Target_Value_3511.Text += k.ToString() + ":" + Send_buf[k].ToString() + "\r\n";
                        //}


                    }
                    break;
                case "ICNA3511_GammaW_GREEN":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x20; //GREEN
                        int j = 1;
                        for (int i = 4; i < (25) * 2 + 4 - 1; i = i + 2)  //25个绑点
                        {
                            Send_buf[i] = Name_2_Value_High("ICNA3511G" + j.ToString());//从R1开始
                            Send_buf[i + 1] = Name_2_Value_Low("ICNA3511G" + j.ToString());
                            j = j + 1;
                        }

                    }
                    break;
                case "ICNA3511_GammaW_BLUE":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x30; //BLUE
                        int j = 1;
                        for (int i = 4; i < (25) * 2 + 4 - 1; i = i + 2)  //25个绑点
                        {
                            Send_buf[i] = Name_2_Value_High("ICNA3511B" + j.ToString());//从R1开始
                            Send_buf[i + 1] = Name_2_Value_Low("ICNA3511B" + j.ToString());
                            j = j + 1;
                        }

                    }
                    break;
                //-----------------------------------------------------------------------
                case "ICNA3511_GammaR_RED":
                    {
                        Setting.TempRecord.ICNA3511CmdType = 0x03;
                        Send_buf[1] = 0x09;
                        Send_buf[3] |= 0x10;
                    }
                    break;
                case "ICNA3511_GammaR_GREEN":
                    {
                        Setting.TempRecord.ICNA3511CmdType = 0x04;
                        Send_buf[1] = 0x09;
                        Send_buf[3] |= 0x20;
                    }
                    break;
                case "ICNA3511_GammaR_BLUE":
                    {
                        Setting.TempRecord.ICNA3511CmdType = 0x05;
                        Send_buf[1] = 0x09;
                        Send_buf[3] |= 0x30;
                    }
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);   //
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }


        //============通过控件名控制对其属性进行控制=====start========
        private Control FindControl(string controlName)
        {
            FieldInfo field = this.GetType().GetField(controlName, BindingFlags.Instance | BindingFlags.NonPublic);
            if (field == null) return null;
            return field.GetValue(this) as Control;
        }
        private byte Name_2_Value_High(string control_name)
        {
            //传入控件名，给控件赋值
            NumericUpDown NumericUpDown_x = (NumericUpDown)this.FindControl(control_name);
            return Convert.ToByte(Convert.ToUInt16(NumericUpDown_x.Value.ToString()) >> 8);
        }
        //低八位
        private byte Name_2_Value_Low(string control_name)
        {
            //传入控件名，给控件赋值
            NumericUpDown NumericUpDown_x = (NumericUpDown)this.FindControl(control_name);
            return Convert.ToByte(Convert.ToUInt16(NumericUpDown_x.Value.ToString()) & 0xff);
        }
        //传入控件名，给控件赋值
        private decimal Name_2_Value_3(string control_name, decimal value)
        {
            //传入控件名，给控件赋值
            NumericUpDown NumericUpDown_x = (NumericUpDown)this.FindControl(control_name);
            return NumericUpDown_x.Value = value;
        }
        //传入控件名，给控件赋值
        private decimal Name_2_Value_2(string control_name, int value)
        {
            //传入控件名，给控件赋值
            NumericUpDown NumericUpDown_x = (NumericUpDown)this.FindControl(control_name);
            return NumericUpDown_x.Value = value;
        }
        private byte Name_2_Value(string control_name)
        {
            //传入控件名，返控件回值
            NumericUpDown NumericUpDown_x = (NumericUpDown)this.FindControl(control_name);
            return Convert.ToByte(Convert.ToUInt16(NumericUpDown_x.Value.ToString()));
        }

        //传入指定字符串给控件的text属性
        private string String_Give_to_NameString(string control_name, string str)
        {
            //传入控件名，给控件赋值
            NumericUpDown NumericUpDown_x = (NumericUpDown)this.FindControl(control_name);
            return NumericUpDown_x.Text = str;
        }

        //============通过控件名控制对其属性进行控制=====stop========
        //Gamma保存
        private void Save_ICNA3511_Gamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = "";
                    //for (int i = 1; i < 28; i++)
                    for (int i = 1; i < 26; i++)     //25个绑点，1-25
                    {
                        str = str + "R" + i.ToString() + "," + Name_2_Value("ICNA3511R" + i.ToString()).ToString() + ","
                                     + "G" + i.ToString() + "," + Name_2_Value("ICNA3511G" + i.ToString()).ToString() + ","
                                     + "B" + i.ToString() + "," + Name_2_Value("ICNA3511B" + i.ToString()).ToString() + "\r\n";
                    }
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
          //Gamma载入
        private void Load_ICNA3511_Gamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                //for (int i = 1; i < 28; i++)
                for (int i = 1; i < 26; i++)  //25个绑点 1-25
                {
                    String_Give_to_NameString("ICNA3511R" + i.ToString(), arrtmp[i * 6 - 5].ToString());
                    String_Give_to_NameString("ICNA3511G" + i.ToString(), arrtmp[i * 6 - 3].ToString());
                    String_Give_to_NameString("ICNA3511B" + i.ToString(), arrtmp[i * 6 - 1].ToString());
                }
            }
        }


        private List<string> ImageType;

        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }

        private void btn_Pic_Load_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            string path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo da = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(da);//递归加载文件夹里的所有图片
            Pic_Box.Text = path;
            //vesa_bmp_path = path;
            textBox2.Text = path;
        }
        string vesa_bmp_path;
        private void ICNA3511_btnPrevPic_Run(string filenames)
        {
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            //byte[] TemFileContent = new byte[1024 * 300];
            byte[] TemFileContent = new byte[512 * 4000 + 100];

            System.Int32 ByteReaded = 0x00;
            try
            {
                string FileName;
                //string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString(); // vesa_bmp_path
                if (Vesa_Check.Checked == true)
                {
                    FileName = vesa_bmp_path + '\\' + filenames;
                }
                else
                {
                    FileName = System.Windows.Forms.Application.StartupPath + "\\Resources\\" + filenames;
                }
                //string FileName = vesa_bmp_path + '\\' + filenames;
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //===========很重要的一句话==========
                //------------20180808---------------
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmHelp.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //==================================
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3);
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }
                    TemFileContent[0] = 0x5A;
                    try
                    {
                        frmHelp.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }
                /*
                else
                {
                    MessageBox.Show("BMP分辨率非法");
                }*/
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }


        private void btnReload_Click(object sender, EventArgs e)    //载入画面按键  选择文件夹
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            string path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);//递归加载文件夹里的所有图片
            textBox2.Text = path;
        }
        /// <summary>
        /// 递归文件夹，把文件夹里所有后缀类似图片的文件，加入listbox
        /// </summary>
        /// <param name="di"></param>
        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))//找到一个图片(如果用户手贱把一个excel文件后缀改成jpeg没办法)
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }
        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            Image img = new Bitmap(selectedImg.Path);
            pictureBox1.Image = img;
        }
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            FileStream aFile;
            Int32 m_txlen = 0;

            int HACT_True;
            int VACT_True;
            int j;


            byte[] TemFileContent = new byte[512 * 4000 + 100];
            int ByteReaded = 0x00;

            string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            if (frmHelp.HACT.Value != HACT_True)
            {
                frmHelp.Text += "图片宽度与设定不符\r\n";
                return;
            }
            if ((frmHelp.BridgeChipModel.SelectedIndex == 1) && (frmHelp.Dis_mode.SelectedIndex == 1))
            // if ((MainForm.Dis_mode.SelectedIndex == 1)) //(Dis_mode.SelectedIndex == 1):MIPI 模式，因为下位机仅对MIPI模式做了优化
            {
                //------------20180903---------------
                try
                {   //bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmHelp.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 0;
                    int x, y, s = 0;
                    x = 512 * 4000; //单次传输数量
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                        y++;
                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                            aFile.Seek((j) * x + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, x);

                            try
                            {
                                frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else
            {
                //------------20180808---------------
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmHelp.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 1;
                    //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                            try
                            {
                                frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            return;
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    Target_Value_3511.Text += "啥图片都没显示";
                    Target_Value_3511.Select(Target_Value_3511.Text.Length, 0);
                    Target_Value_3511.ScrollToCaret();
                }
            }


        }



        public void ICNA3511_OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {
                case "btnPatternW":                             //写入Pattern参数
                    {
                        if (all_checkbox.Checked == true)
                        {
                            numPatternG.Value = numPatternR.Value;
                            numPatternG.Value = numPatternR.Value;
                            Send_buf[1] = 0x07;
                            Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                            Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                            Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                        }
                        else
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                            Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                            Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                        }
                    }
                    break;
                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL

                case "btnPattern255":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 255;
                        Send_buf[4] = 255;
                        Send_buf[5] = 255;
                    }
                    break;
                case "btnPattern224":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 224;
                        Send_buf[4] = 224;
                        Send_buf[5] = 224;
                    }
                    break;

                case "btnPattern192":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 192;
                        Send_buf[4] = 192;
                        Send_buf[5] = 192;
                    }
                    break;
                case "btnPattern128":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 128;
                        Send_buf[4] = 128;
                        Send_buf[5] = 128;
                    }
                    break;
                case "btnPattern96":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 96;
                        Send_buf[4] = 96;
                        Send_buf[5] = 96;
                    }
                    break;
                case "btnPattern64":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 64;
                        Send_buf[4] = 64;
                        Send_buf[5] = 64;
                    }
                    break;
                case "btnPattern32":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 32;
                        Send_buf[4] = 32;
                        Send_buf[5] = 32;
                    }
                    break;
                case "btnPattern16":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 16;
                        Send_buf[4] = 16;
                        Send_buf[5] = 16;
                    }
                    break;
                default:
                    break;


            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }



        }
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private int index = 0;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        private double lum_off, x_off, y_off;
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;

        private int supplement_index = 0;//15-index=supplement_index
        //目标亮度寄存器的数组
        private double[] Target_Value_ICNA3511 = new double[25] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };//第一个是255的目标值点
        //*********每个绑点灰阶亮度浮动范围******************方向是从255--->0************************
        private double[] Gary_Number_ICNA_3511_Lum_off = new double[25] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };//设置每个绑点的off
        private void gamma_set_update_W(EventArgs e)
        {
            ICNA3511_GammaWR_Click(ICNA3511_GammaW_RED, e);
            System.Threading.Thread.Sleep(10);
            ICNA3511_GammaWR_Click(ICNA3511_GammaW_GREEN, e);
            System.Threading.Thread.Sleep(10);
            ICNA3511_GammaWR_Click(ICNA3511_GammaW_BLUE, e);
            System.Threading.Thread.Sleep(10);
        }



        private void ICNA3511_Autogamma_Click(object sender, EventArgs e)
        { //gamma 调节函数

            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            //gamma_set_update_R(e);//回读一次
            D_lums = new double[25, 3];
            D_lum = new double[25];
            index = 0;

            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            for (int i = 0; i < 25; i++)
            {
                D_lum[i] = Target_Lum_Value_ICNA3511[i];
            }

            //根据310出来的色坐标及亮度算R G B寄存器的值
            D_x = Convert.ToDouble(txt_ICNA3511_x.Text);
            D_y = Convert.ToDouble(txt_ICNA3511_y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 25; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt_ICNA3511_loff.Text);
                        x_off = Convert.ToDouble(txt_ICNA3511_xoff.Text);
                        y_off = Convert.ToDouble(txt_ICNA3511_yoff.Text);
                        gamma_set_update_W(e);
                        ICNA3511_OLED_Show_Click(btnPattern255, e);
                        //255亮度

                        T17.Enabled = true;
                    }
                    break;
                default:
                    break;
            }
        }

        double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

        CaSDK Ca310Obj = new CaSDK();//310实例化
                                     //使用310获得测量数据
        private void Msr_CA310()
        {
            //if (stop_flag == 0)
            //{ T18.Enabled = false; }
            //else { }
            try
            {
                frmHelp.Msr_CASDK();
                auto_lum = frmHelp.CASDK_LUM;
                auto_x = frmHelp.CASDK_x;
                auto_y = frmHelp.CASDK_y;
            }
            catch
            {
                MessageBox.Show("请校准Senser！");
                //T17.Enabled = false;
                //return false;
            }

        }

        private void T17_Tick(object sender, EventArgs e)
        {
            Msr_CA310();
            //if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
            //    flag1 = 0;
            //if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
            //    flag2 = 0;
            //if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
            //    flag3 = 0;

            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
            { flag1 = 0; }
            else
            { flag1 = 1; }
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)))//B
            { flag2 = 0; }
            else
            { flag2 = 1; }
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)))//R
            { flag3 = 0; }
            else
            { flag3 = 1; }
            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T17.Enabled = false;
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 24)//0绑点不调节  index==24对应第0个绑点
                {
                    Target_Value_3511.Text = Target_Value_3511.Text + "3511 gamma调试完毕!\r\n";
                }
                if (index < 24)
                {
                    switch (index)
                    {
                        //off 值选定
                        //0对应0；1对应1；2对应2
                        case 0: //255的  
                        case 1: //239的
                        case 2://
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                        case 13:
                        case 14:
                        case 15:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        case 20:
                        case 21:
                        case 22:
                        case 23:
                        case 24:
                            lum_off = Gary_Number_ICNA_3511_Lum_off[index];
                            break;
                    }

                    //注意切灰阶图
                    numPatternR.Value = Target_Node_Value_ICNA3511[24 - index];
                    ICNA3511_OLED_Show_Click(btnPatternW, e);
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T17.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);
                //简单的写法
                switch (index)
                {
                    //                	index==0==>15
                    //                	index==1==>14
                    //                	index==2==>13
                    //                	index==3==>12
                    //                	index==4==>11
                    //                	index==5==>10
                    //                	index==6==>9
                    //                	index==7==>8
                    //                	index==8==>7
                    //                	index==9==>6
                    //                	index==10==>5
                    //                	index==11==>4
                    //                	index==12==>3
                    //                	index==13==>2
                    //                	index==14==>1
                    case 0:    //15
                    case 1:    //14
                    case 2:    //13
                    case 3:    //14
                    case 4:    //11
                    case 5:    //10
                    case 6:    //9
                    case 7:    //8
                    case 8:    //7
                    case 9:    //6
                    case 10:    //5
                    case 11:    //4
                    case 12:    //3
                    case 13:    //2
                    case 14:    //1
                    case 15:
                    case 16:
                    case 17:
                    case 18:
                    case 19:
                    case 20:
                    case 21:
                    case 22:
                    case 23:
                    case 24:
                        supplement_index = 25 - index;
                        gamma_algorithm();
                        break;
                }
                //后续针对单个绑点单独写
                ICNA3511_GammaWR_Click(ICNA3511_GammaW_RED, e);
                ICNA3511_GammaWR_Click(ICNA3511_GammaW_GREEN, e);
                ICNA3511_GammaWR_Click(ICNA3511_GammaW_BLUE, e);
                System.Threading.Thread.Sleep(10);

            }

        }

        private int step_h = 10;
        private int step_l = 1;
        //数组初始化为0
        private int[] Adjust_Array_R_25 = new int[25];
        private int[] Adjust_Array_G_25 = new int[25];
        private int[] Adjust_Array_B_25 = new int[25];
        //gamma算法打包
        private void gamma_algorithm()  //
        {
            //private int supplement_index = 0;//15-index=supplement_index
            if (flag3 == 1)
            {
                if (adjust_Rnum > 10)
                {
                    if (lumr > D_lums[index, 0])
                        Adjust_Array_R_25[supplement_index - 1] = Adjust_Array_R_25[supplement_index - 1] - step_h;
                    else
                        Adjust_Array_R_25[supplement_index - 1] = Adjust_Array_R_25[supplement_index - 1] + step_h;
                    adjust_Rnum = 0;
                    //if (Adjust_Array_R_25[supplement_index - 1] >= Adjust_Array_R_25[supplement_index - 1 + 1])
                    //    Adjust_Array_R_25[supplement_index - 1] = Adjust_Array_R_25[supplement_index - 1 + 1] - 0xFF;
                    adjust_x++;
                    if (adjust_x > 50)
                    {
                        x_off += 0.0005;
                        adjust_x = 0;
                    }
                }
                else
                {
                    if (lumr > D_lums[index, 0])
                        Adjust_Array_R_25[supplement_index - 1] = Adjust_Array_R_25[supplement_index - 1] - step_l;
                    else
                        Adjust_Array_R_25[supplement_index - 1] = Adjust_Array_R_25[supplement_index - 1] + step_l;
                    //if (Adjust_Array_R_25[supplement_index - 1] >= Adjust_Array_R_25[supplement_index - 1 + 1])
                    //    Adjust_Array_R_25[supplement_index - 1] = Adjust_Array_R_25[supplement_index - 1 + 1];
                }
                adjust_Rnum++;
            }
            if (flag1 == 1)
            {
                if (adjust_Gnum > 10)
                {
                    if (lumg > D_lums[index, 1])
                        Adjust_Array_G_25[supplement_index - 1] = Adjust_Array_G_25[supplement_index - 1] - step_h;
                    else
                        Adjust_Array_G_25[supplement_index - 1] = Adjust_Array_G_25[supplement_index - 1] + step_h;
                    adjust_Gnum = 0;
                    //if (Adjust_Array_G_25[supplement_index - 1] >= Adjust_Array_G_25[supplement_index - 1 + 1])
                    //    Adjust_Array_G_25[supplement_index - 1] = Adjust_Array_G_25[supplement_index - 1 + 1] - 0xFF;
                    adjust_l++;
                    if (adjust_l > 50)
                    {
                        lum_off *= 1.5;
                        adjust_l = 0;
                    }
                }
                else
                {
                    if (lumg > D_lums[index, 1])
                        Adjust_Array_G_25[supplement_index - 1] = Adjust_Array_G_25[supplement_index - 1] - step_l;
                    else
                        Adjust_Array_G_25[supplement_index - 1] = Adjust_Array_G_25[supplement_index - 1] + step_l;
                    //if (Adjust_Array_G_25[supplement_index - 1] >= Adjust_Array_G_25[supplement_index - 1 + 1])
                    //    Adjust_Array_G_25[supplement_index - 1] = Adjust_Array_G_25[supplement_index - 1 + 1] - 0xFF;

                }
                adjust_Gnum++;
            }
            if (flag2 == 1)
            {
                if (adjust_Bnum > 10)
                {
                    if (lumb > D_lums[index, 2])
                        Adjust_Array_B_25[supplement_index - 1] = Adjust_Array_B_25[supplement_index - 1] - step_h;
                    else
                        Adjust_Array_B_25[supplement_index - 1] = Adjust_Array_B_25[supplement_index - 1] + step_h;
                    adjust_Bnum = 0;
                    //if (Adjust_Array_B_25[supplement_index - 1] >= Adjust_Array_B_25[supplement_index - 1 + 1])
                    //    Adjust_Array_B_25[supplement_index - 1] = Adjust_Array_B_25[supplement_index - 1 + 1] - 0xFF;
                    adjust_y++;
                    if (adjust_y > 50)
                    {
                        y_off += 0.0005;
                        adjust_y = 0;
                    }
                }
                else
                {
                    if (lumb > D_lums[index, 2])
                        Adjust_Array_B_25[supplement_index - 1] = Adjust_Array_B_25[supplement_index - 1] - step_l;
                    else
                        Adjust_Array_B_25[supplement_index - 1] = Adjust_Array_B_25[supplement_index - 1] + step_l;
                    //if (Adjust_Array_B_25[supplement_index - 1] >= Adjust_Array_B_25[supplement_index - 1 + 1])
                    //    Adjust_Array_B_25[supplement_index - 1] = Adjust_Array_B_25[supplement_index - 1 + 1] - 0xFF;
                }
                adjust_Bnum++;
            }
            //Name_2_Value_2("NT37700C_R" + (supplement_index - 1).ToString(), Adjust_Array_R_25[supplement_index - 1]);
            //Name_2_Value_2("NT37700C_G" + (supplement_index - 1).ToString(), Adjust_Array_G_25[supplement_index - 1]);
            //Name_2_Value_2("NT37700C_B" + (supplement_index - 1).ToString(), Adjust_Array_B_25[supplement_index - 1]);
        }

        private void stop333_Click(object sender, EventArgs e)
        {

        }

        //需要写一个上位机延时
        //demuar 代码相关
        private void Send_Cmd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btnSend1":
                    {
                        frmHelp.txtProcessDrivercode(txtCmd1.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
            }
            Setting.TempRecord.cmdLen = 6;
            if (Write_LP.Checked)
            {
                frmHelp.SendCmd(0);
            }
            else
            {
                frmHelp.SendCmd(4);
            }
            Target_Value_3511.Text += "命令已下发...\r\n";
            Target_Value_3511.Select(Target_Value_3511.Text.Length, 0);
            Target_Value_3511.ScrollToCaret();
        }
        private void btn_Delta_set_Click(object sender, EventArgs e)
        {
            //0. Delta RGB Mode Setting
            txtCmd1.Text = "";
            txtCmd1.Text = "9F,0A\r\n"
            + "B7,13,29\r\n";
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Demura_set_Click(object sender, EventArgs e)
        {///1. De-Mura Setting (External Flash “WINBOND Device")
            txtCmd1.Text = "";
            txtCmd1.Text = "9F,0D\r\n"
 + "B2,31,32,30,33,01,0D,00,00,00,00,09,83,09,83,00,00,00\r\n"
 + "B3,33,23,22,20\r\n"
 + "B4,0A,03\r\n"
 + "E0,01\r\n"
 + "E3,36\r\n"
 + "E2,00,3F\r\n";
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Flsah_EN_Click(object sender, EventArgs e)
        {
            txtCmd1.Text = "";
            txtCmd1.Text = "E2,10\r\n"
+ " E2,00\r\n"
+ "E1,06,00,00,00,00,00\r\n"
+ "E2,08\r\n"
+ "E2,00\r\n"
+ "E1,31,00,00,02,00,00\r\n"
+ "E2,08\r\n"
+ "E2,00\r\n"
+ "E1,35,00,00,00,00,00\r\n"
+ "E2,08\r\n"
+ "E2,00\r\n"
+ "E6 #4\r\n"//回读
+ "E1,06,00,00,00,00,00\r\n"
+ "E2,08\r\n"
+ "E2,00\r\n"
+ "E1,9F,00,00,00,00,00\r\n"
+ "E2,08\r\n"
+ "E2,00\r\n";
            //回读        RE6 #4
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Flash_Era_Click(object sender, EventArgs e)
        {//3.External Flash Erase
            txtCmd1.Text = "";
            txtCmd1.Text = " E1,06,00,00,00,00,00\r\n"
            + "E2,08\r\n"
            + "E2,00\r\n"
            + "E1,60,00,00,00,00,00\r\n"
            + "E2,08\r\n"
            + "E2,00\r\n"
            + "delay,30000\r\n"
            //4.External Flash Erase Check
            + "E2,01\r\n"
            + "delay 3000\r\n"
            + "E2,00\r\n"
            + "E7 #13\r\n";//需要改改
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Demura_Wr_Click(object sender, EventArgs e)
        {
            txtCmd1.Text = "";
            txtCmd1.Text = " E0,11,01,00\r\n"
                + "delay 20\r\n";
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Send_bin_Click(object sender, EventArgs e)
        {
            //发送bin文件
        }
        private void btn_Demura_Wr_EN_Click(object sender, EventArgs e)
        {
            txtCmd1.Text = "";
            txtCmd1.Text = "E0,01,00,00\r\n" // Demura Ran Write Enable
           + "delay 20\r\n";
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Flash_Wr_Click(object sender, EventArgs e)
        {
            txtCmd1.Text = "";
            txtCmd1.Text = "E2,02\r\n"
               + "delay 30000\r\n"
                + "E2,00\r\n";
            Send_Cmd_Click(btnSend1, e);
        }
        private void btn_Flash_Che_Click(object sender, EventArgs e)
        {
            txtCmd1.Text = "";
            txtCmd1.Text = "E2,01\r\n"
                + "delay 3000\r\n"
                + "E2,00\r\n"
                + "E7 #13\r\n ";
            Send_Cmd_Click(btnSend1, e);
        }

        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            //textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            Target_Value_3511.Focus();//获取焦点
            Target_Value_3511.Select(Target_Value_3511.TextLength, 0);//光标定位到文本最后
            Target_Value_3511.ScrollToCaret();//滚动到光标处
        }



    }
}
